package com.oritsh.dicom.wado.controller;

import com.oritsh.dicom.exception.DicomException;
import com.oritsh.dicom.service.CStoreService;

import org.dcm4che.data.Attributes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;

import javax.sound.sampled.*;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: zarra
 * Date: 14-3-19
 * Time: 下午9:38
 * Copyright Shanghai Orient Rain Information Technology Co.,Ltd.
 */
@RequestMapping("/cstore")
@Controller
public class  CStoreController {

    @Autowired
    CStoreService cStoreService;

    @RequestMapping(value="/upload")
    public @ResponseBody Attributes uploadImage(@RequestPart("dataset") Attributes dataset,
                           @RequestPart("image") MultipartFile file) throws IOException, DicomException {
        BufferedImage image = ImageIO.read(file.getInputStream());
        return cStoreService.cstore(image,dataset);
    }
    
    @RequestMapping(value="/uploadaudio")
    public @ResponseBody Attributes uploadAudio(@RequestPart("dataset") Attributes dataset,
                           @RequestPart("audio") MultipartFile file) throws IOException, DicomException {
        AudioInputStream sound = null;
		try {
			sound = AudioSystem.getAudioInputStream(file.getInputStream());
		} catch (UnsupportedAudioFileException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return cStoreService.cstore(sound,dataset);
    }
}
